Problem with audio


#1

I followed the book and have been successful from chap 1 to 12. But on chap13, I don’t get any sound out of the virtual machine (I just used the default Nexus 7 virtual device that comes with Eclipse).

I tried to solve the challenge on playing video. The code works fine. I could see the video being played. But again, no sound. Did I miss something? I have a feeling that the problem may not be with the code (attached below). I tried maximize the volume on both the vm and the actual machine, still no sound. Can anyone help?

HelloMoonFragment.java:

package com.example.hellomoonChallenge;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.VideoView;

public class HelloMoonFragment extends Fragment {
protected static final String TAG = “HelloMoonVideo”;
private Button mPlayButton;
private Button mStopButton;
private Uri targetUri;
private VideoView mVideoView;

// No overriding of onCreate()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
	View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

	mVideoView = (VideoView) v.findViewById(R.id.hellomoon_videoview);
	
	targetUri = Uri.parse("android.[resource://](resource://)" +
					"com.example.hellomoonChallenge/raw/apollo_17_stroll");
	
	mVideoView.setVideoURI(targetUri);		
	
	
	mPlayButton = (Button) v.findViewById(R.id.hellomoon_playButton);
	mPlayButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.d(TAG, "starting to play");
			if ( !mVideoView.isPlaying()) {
				mVideoView.start();
			}
		}
	});
	
	mStopButton = (Button) v.findViewById(R.id.hellomoon_stopButton);
	mStopButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if (mVideoView.isPlaying()) {
				Log.d(TAG, "Stopping");
				mVideoView.stopPlayback();
			}
		}
	});		
	return v;
}

@Override
public void onDestroy() {
	super.onDestroy();
	mVideoView.stopPlayback();
}

}

fragment_hello_moon.xml

<?xml version="1.0" encoding="utf-8"?>

<VideoView 
	    android:id="@+id/hellomoon_videoview"
   	 	android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_weight="1"
/>

<TableRow
    android:gravity="center|bottom"
    android:layout_weight="0" >
    
    <Button 
    	android:id="@+id/hellomoon_playButton"
    	android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/hellomoon_play"
    />
    <Button 
    	android:id="@+id/hellomoon_stopButton"
    	android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/hellomoon_stop"
    />
    
</TableRow>

#2

I to had the same issue at the beginning. I found out i totally mist this line of code:

mPlayer.start();

its in:

private void start(Context c){
stop();

     mPlayer = MediaPlayer.create(c, R.raw.one_small_step);
	
     mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
		public void onCompletion(MediaPlayer mp) {
			stop();
		}
     });

    [b] mPlayer.start();[/b]

}


#3

I’m really running into a problem with this chapter 13. I made all the changes and as soon as I enhanced the Button logic in HelloMoonFragment, the process immediately abends. To verify if I forgot anything, I compared all the individual programs with the answers in 13_HelloMoon and it’s continues to abend. 13_HelloMoon works fine in the animater. I’m running out of possible solutions for this. It seems to abend when trying to create an Activity in HelloMoonActivity.


#4

FYI Here’s my source code. If anyone sees anything that I should change please let me know:

in com.bignerdranch.hellomoon/src

AudioPlayer.java:

package com.bignerdranch.hellomoon;

import android.content.Context;
import android.media.MediaPlayer;

public class AudioPlayer {

private MediaPlayer mPlayer;

public void stop() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}

public void play(Context c) {
		
	stop();
		
	mPlayer = MediaPlayer.create(c, R.raw.one_small_step);
		
	mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
		public void onCompletion(MediaPlayer mp) {
			stop();
		}
	});
		
	mPlayer.start();
}
  public boolean isPlaying() {
       return mPlayer != null;
   }
    
}

HelloMoonActivity:

package com.bignerdranch.hellomoon;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class HelloMoonActivity extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_hello_moon);
}

}

HelloMoonFragment:

package com.bignerdranch.hellomoon;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HelloMoonFragment extends Fragment {
private AudioPlayer mPlayer = new AudioPlayer();

private Button mPlayButton;
private Button mStopButton;

void updateButtons() {
    boolean isEnabled = !mPlayer.isPlaying();
    mPlayButton.setEnabled(isEnabled);
}

@Override
public void onDestroy() {
	super.onDestroy();
	mPlayer.stop();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
	View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
	
	mPlayButton = (Button)v.findViewById(R.id.hellomoon_playButton);
	mStopButton = (Button)v.findViewById(R.id.hellomoon_stopButton);
	
	mPlayButton.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			mPlayer.play(getActivity());
			updateButtons();
		}
	}); 
	
	mStopButton.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			mPlayer.stop();
			updateButtons();
		}
	});
	
	return v; 
}

}

in /layout

activity_hello_moon.xml:

<?xml version="1.0" encoding="utf-8"?>

fragment_hello_moon.xml:

<?xml version="1.0" encoding="utf-8"?>







in /menu

activity_hello_moon.xml

<?xml version="1.0" encoding="utf-8"?>
<item
    android:id="@+id/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/menu_settings"/>

in /values

<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">HelloMoon</string>
<string name="menu_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="hellomoon_play">Play</string>
<string name="hellomoon_stop">Play</string>
<string name="hellomoon_description">Neil Armstrong stepping onto the moon</string>
<string name="hellomoon_image_description">Neil Armstrong stepping 
        onto the moon</string>

in AndroidManifest,xml:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="8"
          android:targetSdkVersion="17" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".HelloMoonActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

#5

Could you post your code using the [code] tag? It is very difficult to read without proper indentation.


#6

Phillips How do I do that? Is there a URL where I can find instructions on this?


#7

Is this what you mean?

[code]package com.bignerdranch.hellomoon;

import android.content.Context;
import android.media.MediaPlayer;

public class AudioPlayer {

private MediaPlayer mPlayer;

public void stop() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}

public void play(Context c) {
		
	stop();
		
	mPlayer = MediaPlayer.create(c, R.raw.one_small_step);
		
	mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
		public void onCompletion(MediaPlayer mp) {
			stop();
		}
	});
		
	mPlayer.start();
}
  public boolean isPlaying() {
       return mPlayer != null;
   }
    
}

[/code]

[code]package com.bignerdranch.hellomoon;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class HelloMoonActivity extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_hello_moon);
}

}[/code]

[code]package com.bignerdranch.hellomoon;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HelloMoonFragment extends Fragment {
private AudioPlayer mPlayer = new AudioPlayer();

private Button mPlayButton;
private Button mStopButton;

void updateButtons() {
    boolean isEnabled = !mPlayer.isPlaying();
    mPlayButton.setEnabled(isEnabled);
}

@Override
public void onDestroy() {
	super.onDestroy();
	mPlayer.stop();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
	View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
	
	mPlayButton = (Button)v.findViewById(R.id.hellomoon_playButton);
	mStopButton = (Button)v.findViewById(R.id.hellomoon_stopButton);
	
	mPlayButton.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			mPlayer.play(getActivity());
			updateButtons();
		}
	}); 
	
	mStopButton.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			mPlayer.stop();
			updateButtons();
		}
	});
	
	return v; 
}

}
[/code]

[code]<?xml version="1.0" encoding="utf-8"?>

[/code]

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:src="@drawable/armstrong_on_moon" android:contentDescription="@string/hellomoon_image_description" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerInside" android:layout_weight="1" /> <TableRow android:gravity="center|bottom" android:layout_weight="0" > <Button android:src="@+id/hellomoon_playButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hellomoon_play" /> <Button android:src="@+id/hellomoon_stopButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hellomoon_stop" /> </TableRow> </TableLayout>

[code]<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/menu_settings"/>
[/code]

[code]<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">HelloMoon</string>
<string name="menu_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="hellomoon_play">Play</string>
<string name="hellomoon_stop">Play</string>
<string name="hellomoon_description">Neil Armstrong stepping onto the moon</string>
<string name="hellomoon_image_description">Neil Armstrong stepping 
        onto the moon</string>
[/code]

[code]<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="8"
          android:targetSdkVersion="17" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".HelloMoonActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
[/code]

#8

That’s it, exactly. The [code] tag preserves indentation.


#9

Could you also show the exception stack trace in LogCat that you see when the program crashes?


#10

Here’s the Logcat/ Thanks for your help.

09-04 02:17:11.649: D/AndroidRuntime(275): Shutting down VM 09-04 02:17:11.659: W/dalvikvm(275): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 09-04 02:17:11.689: E/AndroidRuntime(275): FATAL EXCEPTION: main 09-04 02:17:11.689: E/AndroidRuntime(275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.hellomoon/com.bignerdranch.hellomoon.HelloMoonActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 09-04 02:17:11.689: E/AndroidRuntime(275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.os.Handler.dispatchMessage(Handler.java:99) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.os.Looper.loop(Looper.java:123) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-04 02:17:11.689: E/AndroidRuntime(275): at java.lang.reflect.Method.invokeNative(Native Method) 09-04 02:17:11.689: E/AndroidRuntime(275): at java.lang.reflect.Method.invoke(Method.java:521) 09-04 02:17:11.689: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-04 02:17:11.689: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-04 02:17:11.689: E/AndroidRuntime(275): at dalvik.system.NativeStart.main(Native Method) 09-04 02:17:11.689: E/AndroidRuntime(275): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 09-04 02:17:11.689: E/AndroidRuntime(275): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.view.LayoutInflater.inflate(LayoutInflater.java:385) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 09-04 02:17:11.689: E/AndroidRuntime(275): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.app.Activity.setContentView(Activity.java:1647) 09-04 02:17:11.689: E/AndroidRuntime(275): at com.bignerdranch.hellomoon.HelloMoonActivity.onCreate(HelloMoonActivity.java:11) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 09-04 02:17:11.689: E/AndroidRuntime(275): ... 11 more 09-04 02:17:11.689: E/AndroidRuntime(275): Caused by: java.lang.NullPointerException 09-04 02:17:11.689: E/AndroidRuntime(275): at com.bignerdranch.hellomoon.HelloMoonFragment.onCreateView(HelloMoonFragment.java:34) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285) 09-04 02:17:11.689: E/AndroidRuntime(275): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558) 09-04 02:17:11.689: E/AndroidRuntime(275): ... 19 more 09-04 02:17:11.719: W/ActivityManager(58): Force finishing activity com.bignerdranch.hellomoon/.HelloMoonActivity


#11

It looks like your buttons don’t have id’s set.


#12

Phillip, I found it. I accidentally had “android:src” instead of “android:id” on each Button in my fragment_hello_moon.xml. Can’t belive I didn’t see it. Thanks for your help:)


#13

So its complaining that it can’t inflate a fragment. You only inflate a fragment in one spot: inside activity_hello_moon.XML

Peaking around your code there it looks like you forgot the “.android” in the name field.

As a side note: play around with using github gists. They make sharing code snippets REAL easy.


#14

What is github gists?