如圖,按照書上的代碼撰寫了一個音樂播放器,但是加載到手機后播放不了音樂(模擬機上也不行),一開始是出現下圖這種情況,無法build.按照網上的說法,把那個targetsdkversion 改成26之后,能夠build .但是播放不了音樂。求助啊求助!!謝謝!萬分感謝!
uj5u.com熱心網友回復:
package com.example.chinayexin.player;import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements
MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener{
Uri uri;
TextView txvName ,txvUri;
boolean isVideo = false;
Button btnPlay,btnStop;
CheckBox ckbLoop;
MediaPlayer mper = new MediaPlayer();
Toast tos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//設定螢屏不轉向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
txvName =(TextView) findViewById(R.id.txvName);
txvUri = (TextView) findViewById(R.id.txvUri);
btnPlay=(Button) findViewById(R.id.btnPlay);
btnStop= (Button) findViewById(R.id.btnStop);
ckbLoop=(CheckBox) findViewById(R.id.ckbLoop);
uri= Uri.parse("android.resource://"+getPackageCodePath()+"/"+R.raw.music);
txvName.setText("music.mp3");
txvUri.setText("程式內音樂:"+uri.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("播放");
btnPlay.setEnabled(false);
btnStop.setEnabled(false);
try{
mper.reset();
mper.setDataSource(this,uri);
mper.setLooping( ckbLoop.isChecked());
mper.prepareAsync();
} catch (IOException e) {
tos.setText("指定音樂錯誤"+e.toString());
tos.show();
}
}
public void onPick(View view) {
Intent it = new Intent(Intent .ACTION_GET_CONTENT);
if(view.getId()==R.id.btnPickAudio){
it.setType("audio/*");
startActivityForResult(it,100);
}
else{
it.setType("Vedio/*");
startActivityForResult(it,101);
}
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(resultCode== Activity.RESULT_OK){
isVideo= (requestCode==101);
uri= convertUri(data.getData());
txvName.setText(uri.getLastPathSegment());
txvUri.setText("檔案位置:"+uri.getPath());
if(!isVideo)prepareMusic();
}
}
private Uri convertUri(Uri uri) {
if(uri.toString().substring(0,7).equals("content")){
String[] colName = {MediaStore.MediaColumns.DATA};
Cursor cursor = getContentResolver()
.query(uri,colName,null,null,null);
cursor.moveToFirst();
uri=Uri.parse("file://"+cursor.getString(0));
cursor.close();
}
return uri ;
}
@Override
public void onCompletion(MediaPlayer mp) {
mper.seekTo(0);
btnPlay.setText("播放");
btnStop.setEnabled(false);
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
tos.setText("發生錯誤,停止播放");
tos.show();
return true;
}
@Override
public void onPrepared(MediaPlayer mp) {
btnPlay.setEnabled(true);
}
public void onMpPlay(View view) {
if(mper.isPlaying()){
mper.pause();
btnPlay.setText("繼續");
}
else{
mper.start();
btnPlay.setText("暫停");
btnStop.setEnabled(true);
}
}
public void onMpStop(View view) {
mper.pause();
mper.seekTo(0);
btnPlay.setText("播放");
btnStop.setEnabled(false);
}
public void onMpLoop(View view) {
if (ckbLoop.isChecked())
mper.setLooping(true);
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;
mper.seekTo(pos);
tos.setText("快進10秒:"+pos/1000+"/"+len/1000);
tos.show();
}
public void onMpBackward(View view) {
if(!btnPlay.isEnabled())return;
int len=mper.getDuration();
int pos=mper.getCurrentPosition();
pos-=10000;
if(pos<0)pos=0;
mper.seekTo(pos);
tos.setText("快退10秒"+pos/1000+"/"+len/1000);
tos.show();
}
@Override
protected void onPause() {
super.onPause();
if(mper.isPlaying()){
btnPlay.setText("繼續");
mper.pause();
}
}
@Override
protected void onDestroy() {
mper.release();
super.onDestroy();
}
}
uj5u.com熱心網友回復:
提示很明顯,在APP的 build.grade 檔案里面的 com.android.support:media2:28.0.0 找不到,或許你可以把 28.0.0 版本號降低一點 27.0.2 或者其它轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/133437.html
標籤:Android
上一篇:ERROR: Could not find method implementationSdkVersion() for arguments [28] on ob
