Playing Video Stuck


#1

I get the following runtime error

06-23 03:08:38.664: E/AndroidRuntime(869): FATAL EXCEPTION: main
06-23 03:08:38.664: E/AndroidRuntime(869): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.hellomoon/com.bignerdranch.android.hellomoon.HelloMoonActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
06-23 03:08:38.664: E/AndroidRuntime(869): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

Any ideas how to get this running?

package com.bignerdranch.android.hellomoon;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

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.apollo_17_stroll);
		
		mPlayer.start();
		
		mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				stop();
				
			}
		});

		mPlayer.start();
	}
	
	public void pause()
	{
		if (mPlayer != null)
		{
			if (mPlayer.isPlaying()) { mPlayer.pause();}
			else mPlayer.start();
			
		}
	}

}
package com.bignerdranch.android.hellomoon;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.media.MediaPlayer;

public class HelloMoonFragment extends Fragment {

	private AudioPlayer mPlayer = new AudioPlayer();
	
	
	private SurfaceView view;
	private SurfaceHolder holder;
	private MediaPlayer mp;
	
	private Button mPlayButton;
	private Button mStopButton;
	private Button mPause;
	
	@Override
	public View onCreateView(LayoutInflater inflater,  ViewGroup parent, Bundle savedInstanceState)
	{
		View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
		view = (SurfaceView)v.findViewById(R.id.surface);
		holder = view.getHolder();
		holder.setKeepScreenOn (true); 
		mp.setLooping (true);
		
		mPlayButton = (Button)v.findViewById(R.id.hellomoon_playButton);
		
		mPlayButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mPlayer.play(getActivity());
				
			}
		});
		mPause = (Button)v.findViewById(R.id.hellomoon_pauseButton);
		
		mPause.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mPlayer.pause();
			}
		});
		
		mStopButton = (Button)v.findViewById(R.id.hellomoon_stopButton);
		
		mStopButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mPlayer.stop();				
			}
		});
		
		return v;
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		mPlayer.stop();
	}

}
<?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">
    
  <SurfaceView
    android:id="@+id/surface"
    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:id="@+id/hellomoon_playButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/hellomoon_play" 
      />
    
    <Button android:id="@+id/hellomoon_pauseButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/hellomoon_pause"
      />
    
    <Button android:id="@+id/hellomoon_stopButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/hellomoon_stop"
      />
   </TableRow>

</TableLayout>

#2

You’re not assigning the SurfaceHolder to the MediaPlayer.

In AudioPlayer, add a private member to hold the surface:

	private SurfaceHolder mSurfaceHolder;

And a public accessor to set it:

	public void setSurface(SurfaceHolder sh) {
		mSurfaceHolder = sh;
	}

And assign the SurfaceHolder after instantiating the MediaPlayer in play():

		mPlayer = MediaPlayer.create(c, R.raw.apollo_17_stroll);
		mPlayer.setDisplay(mSurfaceHolder);

Then back in In HelloMoonFragment, set the SurfaceHolder after creating the view in onCreateView():

		View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
		
		SurfaceView mSurface = (SurfaceView)v.findViewById(R.id.surface);
		mPlayer.setSurface(mSurface.getHolder());