Reputation: 344
I do simple small programs in android for my practice. While I am on the way to create a simple player app, I had to face an error which I could not solve.The following is my code.
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AndrmusiActivity extends Activity {
/** Called when the activity is first created. */
public MediaPlayer playr;
public Button b1;
public Button b2;
public Button b3;
@Override
public void onCreate(Bundle State) {
super.onCreate(State);
setContentView(R.layout.main);
b1= (Button)findViewById(R.id.play);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playr = MediaPlayer.create(this,R.raw.showme);
playr.start();
}
});
b2= (Button)findViewById(R.id.Pause);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v1) {
playr.pause();
}
});
b3=(Button)findViewById(R.id.Stop);
b3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playr.stop();
playr.reset();
}
});
}
}
Now I faced the error at play method at the line
playr = MediaPlayer.create(this,R.raw.showme);
Could any one please help me in this aspect. Thanks in adv
Upvotes: 0
Views: 13432
Reputation: 450
use this code to create mediaplayer
context
playr = MediaPlayer.create(getBaseContext(),R.raw.showme);
Upvotes: 1
Reputation: 101
This is my activity example to play videos in Android using VideoView
:
package br.com.player;
import android.support.v7.app.AppCompatActivity;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
public class PlayerActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
Button buttonPlayVideo = (Button)findViewById(R.id.playvideobuttom);
getWindow().setFormat(PixelFormat.UNKNOWN);
VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
String uriPath = "android.resource://br.com.player/"+R.raw.filevideo;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
}});
}
}
And this is activity's xml player.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/playvideobuttom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play Video"
/>
<VideoView
android:id="@+id/videoview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Upvotes: 0
Reputation: 20041
playr = MediaPlayer.create(AndrmusiActivity.this,R.raw.showme);
If error remains post your error logcat.
create this in your on create() method.
playr = MediaPlayer.create(this,R.raw.showme);
use your your code as same as below.
b1 = (Button) findViewById(R.id.play);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playr.start();
}
});
Calling start() to resume playback for a paused MediaPlayer object, and the resumed playback position is the same as where it was paused. When the call to start() returns, the paused MediaPlayer object goes back to the Started state.
http://developer.android.com/reference/android/media/MediaPlayer.html#start()
Upvotes: 5
Reputation: 22822
Replace
playr = MediaPlayer.create(this,R.raw.showme);
with
playr = MediaPlayer.create(AndrmusiActivity.this,R.raw.showme);
Upvotes: 1