Home > Mobile >  Android studio white development for help
Android studio white development for help

Time:10-22

As shown in figure, according to the book of coding a music player, but after loaded into the mobile phone can't play music (not simulator), the beginning is appear below this kind of situation, can't build. According to the Internet, after changing the targetsdkversion to 26, can build. But don't play music, turn to for help!! Thank you very much! Thank you very much!

CodePudding user response:

Package com. Example. Chinayexin. Player;

import android.app.Activity;
import android.content.Intent;
The import android. Content. PM. ActivityInfo;
The import android. Database. Cursor;
The import android. Media. MediaPlayer;
The import android.net.Uri;
import android.os.Bundle;
The import android. The provider. MediaStore;
The import android. Support. V7. App. AppCompatActivity;
import android.view.View;
The import android. View. WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
The import android. Widget. TextView;
import android.widget.Toast;

import java.io.IOException;

Public class MainActivity extends AppCompatActivity implements
MediaPlayer OnPreparedListener,
MediaPlayer one rrorListener,
MediaPlayer. OnCompletionListener {

Uri the Uri;
TextView txvName txvUri;
Boolean isVideo=false;
The Button btnPlay btnStop;
The CheckBox ckbLoop;
MediaPlayer mper=new MediaPlayer ();
Toast tos.
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//set the screen don't turn to
SetRequestedOrientation (ActivityInfo SCREEN_ORIENTATION_NOSENSOR);
SetRequestedOrientation (ActivityInfo SCREEN_ORIENTATION_PORTRAIT);

(WindowManager getWindow (). AddFlags. LayoutParams. FLAG_KEEP_SCREEN_ON);

TxvName=(TextView) the findViewById (R.i which xvName);
TxvUri=(TextView) the findViewById (R.i which xvUri);
BtnPlay=(Button) the findViewById (R.i db tnPlay);
BtnStop=(Button) the findViewById (R.i db tnStop);
CkbLoop=(CheckBox) the findViewById (R.i, dc kbLoop);

Uri=uri. Parse (" android resource://"+ getPackageCodePath () +"/" + R.r aw. Music);
TxvName. SetText (" music mp3 ");
TxvUri. SetText (" program in music: "+ uri. The toString ());

Mper=new MediaPlayer ();
Mper. SetOnPreparedListener (this);
Mper. SetOnErrorListener (this);
Mper. SetOnCompletionListener (this);
Tos=Toast. MakeText (this, "", Toast. LENGTH_SHORT);

PrepareMusic ();
}

Void prepareMusic () {
BtnPlay. SetText (" play ");
BtnPlay. SetEnabled (false);
BtnStop. SetEnabled (false);
Try {
Mper. Reset ();
Mper. SetDataSource (this uri);
Mper. SetLooping (ckbLoop isChecked ());
Mper. PrepareAsync ();
} the catch (IOException e) {
Tos. SetText (" designated music error "+ e. oString ());
Tos. The show ();
}


}


Public void onPick View (View) {
The Intent of it=new Intent (Intent. ACTION_GET_CONTENT);
If (the getId ()==R.i db tnPickAudio) {
It. SetType (" audio/* ");
StartActivityForResult (it, 100);
}
The else {
It. SetType (" Vedio/* ");
StartActivityForResult (it, 101);
}
}
Protected void onActivityResult (int requestCode, int the resultCode, Intent data) {
Super. OnActivityResult (requestCode, the resultCode, data);
If (the resultCode==Activity. RESULT_OK) {
IsVideo=(requestCode==101);
Uri=convertUri (data. GetData ());
TxvName. SetText (uri) getLastPathSegment ());
TxvUri. SetText (" file location: "+ uri. GetPath ());

if(! IsVideo) prepareMusic ();
}
}

Private Uri convertUri (Uri Uri) {
If (uri. The toString (). The substring (0, 7). The equals (" content ")) {
String [] colName={MediaStore. MediaColumns. DATA};
Cursor Cursor=getContentResolver ()
Query (uri, colName, null, null, null);
Cursor. MoveToFirst ();
Uri=uri. Parse (" file://"+ cursor. Get string (0));
Cursor. The close ();
}
Return the uri;
}

@ Override
Public void onCompletion MediaPlayer (mp) {
Mper. SeekTo (0);
BtnPlay. SetText (" play ");
BtnStop. SetEnabled (false);
}

@ Override
Public Boolean one rror (MediaPlayer mp, int what, int extra) {
Tos. SetText (" error, stop to play ");
Tos. The show ();

return true;
}

@ Override
Public void onPrepared (MediaPlayer mp) {
BtnPlay. SetEnabled (true);
}

Public void onMpPlay View (View) {
If (mper isPlaying ()) {
Mper. Pause ();
BtnPlay. SetText (" to ");
}
The else {
Mper. Start ();
BtnPlay. SetText (" pause ");
BtnStop. SetEnabled (true);
}

}

Public void onMpStop View (View) {
Mper. Pause ();
Mper. SeekTo (0);
BtnPlay. SetText (" play ");
BtnStop. SetEnabled (false);
}

Public void onMpLoop View (View) {
If (ckbLoop isChecked ())
Mper. SetLooping (true);
The else
Mper. SetLooping (false);
}

Public void onMpForward View (View) {
if(! BtnPlay. IsEnabled ()) return;
Int len=mper. GetDuration ();
Int pos=mper. GetCurrentPosition ();
Pos +=10000;
If (pos> Len) pos=len;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related