我制作了一个Android应用程序,可在单击按钮时播放声音。这些声音也可以通过信使共享。所有这些按钮(它们的外观和位置)都在activity_main.xml中指定。问题是,当我在我的应用程序中向上或向下滚动时,它看起来很糟糕,因为它不顺畅,我的应用程序似乎有点滞后。
这是我的代码,其中包含一些按钮和图像视图,例如
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/r1">
<Button
android:text="@string/sound1"
android:layout_marginTop="33dp"
android:id="@+id/sound1"
android:layout_width="210dp"
android:background="@drawable/backgroundbuttons"
android:layout_height="50dp"
android:layout_alignRight="@+id/sound3"
android:layout_alignEnd="@+id/sound3"
android:textSize="13sp" />
<Button
android:text="@string/sound2"
android:layout_width="210dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:id="@+id/sound2"
android:background="@drawable/backgroundbuttons"
android:layout_below="@+id/sound1"
android:layout_alignLeft="@+id/sound1"
android:layout_alignStart="@+id/sound1"
android:textSize="13sp" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
app:srcCompat="@drawable/sharebutton"
android:id="@+id/share_button1"
android:layout_alignBottom="@+id/sound1"
android:layout_toRightOf="@+id/sound1"
android:layout_toEndOf="@+id/sound1" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
app:srcCompat="@drawable/sharebutton"
android:id="@+id/share_button2"
android:layout_alignBottom="@+id/sound2"
android:layout_toRightOf="@+id/sound2"
android:layout_toEndOf="@+id/sound2" />
我的activity_main中总共有 25个按钮和25个图像视图,如下所示。所有这些图像视图都基于一个文件( sharebutton.jpg )。
修改 主要活动类:
public class MainActivity extends AppCompatActivity {
private MediaPlayer mp;
private MessengerThreadParams mThreadParams;
private boolean mPicking;
private static final int REQUEST_CODE_SHARE_TO_MESSENGER = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button s1 = (Button) findViewById(R.id.sound1);
Button s2 = (Button) findViewById(R.id.sound2);
findViewById(R.id.share_button1).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
onMessengerButtonClicked1();
}
});
findViewById(R.id.share_button2).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
onMessengerButtonClicked2();
}
});
Intent intent = getIntent();
if (Intent.ACTION_PICK.equals(intent.getAction())) {
mThreadParams = MessengerUtils.getMessengerThreadParamsForIntent(intent);
mPicking = true;
}
s1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPause();
mp = MediaPlayer.create(MainActivity.this, R.raw.a1);
mp.start();
}
});
s2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPause();
mp = MediaPlayer.create(MainActivity.this, R.raw.a2);
mp.start();
}
private void onMessengerButtonClicked1() {
// The URI can reference a file://, content://, or android.resource.
Uri uri =
Uri.parse("android.resource://" + getPackageName()+ "/raw/" +R.raw.a1);
ShareToMessengerParams shareToMessengerParams =
ShareToMessengerParams.newBuilder(uri, "audio/mpeg")
.setMetaData("{ \"audio\" : \"a1\" }")
.build();
if (mPicking) {
MessengerUtils.finishShareToMessenger(this, shareToMessengerParams);
} else {
MessengerUtils.shareToMessenger(
this,
REQUEST_CODE_SHARE_TO_MESSENGER,
shareToMessengerParams);
}
}
private void onMessengerButtonClicked2() {
Uri uri =
Uri.parse("android.resource://" + getPackageName()+ "/raw/" +R.raw.a2);
ShareToMessengerParams shareToMessengerParams =
ShareToMessengerParams.newBuilder(uri, "audio/mpeg")
.setMetaData("{ \"audio\" : \"a2\" }")
.build();
if (mPicking) {
MessengerUtils.finishShareToMessenger(this, shareToMessengerParams);
} else {
MessengerUtils.shareToMessenger(
this,
REQUEST_CODE_SHARE_TO_MESSENGER,
shareToMessengerParams);
}
}
protected void onPause() {
super.onPause();
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
答案 0 :(得分:0)
https://developer.android.com/guide/components/activities/activity-lifecycle.html
以上是可以在包括您的情况在内的广泛领域中使用的概要。
至于您的代码,您可以实现以下内容:
@Override
public void onResume() {
mp.onResume();
// Setup the player
mp.resume();
}
@Override
public void onDestroy() {
if(mp != null) {
mp.release();
}
super.onDestroy();
}
s1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mp != null)
{
mp.stop();
mp.release();
}
finish();
}
});
}
如果有帮助,请使用日志来帮助您查看生命周期
try{
if(mp !=null && mp.isPlaying()){
Log.d("TAG------->", "player is running");
mp.stop();
Log.d("Tag------->", "player is stopped");
mp.release();
Log.d("TAG------->", "player is released");
}
}catch(Exception e){
//Throw the error
}