我在Android应用程序(我的手机Android 6.0.1 api 23)上工作 应该在悬停时发出声音,在发布时停止播放。它不起作用。是因为我的硬件吗?或者我的代码错了?
JAVA:
import android.media.MediaPlayer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class DisplayActivity extends AppCompatActivity {
private MediaPlayer player;
@Override
protected void onResume() {
super.onResume();
player = MediaPlayer.create(this, R.raw.cena);
player.setLooping(true);
}
@Override
protected void onPause() {
super.onPause();
player.release();
player = null;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.hello).setOnHoverListener(new View.OnHoverListener() {
@Override
public boolean onHover(
View v,
MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
player.start();
break;
case MotionEvent.ACTION_HOVER_EXIT:
player.pause();
player.seekTo(0);
break;
}
return true;
}
});
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="com.example.android.testonhover.MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="@+id/hello"
android:layout_height="wrap_content"
android:text="Hover me!"
android:layout_centerInParent="true"/>
</RelativeLayout>
你能解释一下我该怎样做才能让它发挥作用吗?